﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kaiser.Models.JsonModels;
//using Kaiser.Modules.CMS.Services;
using Kaiser.Models;
//using Kaiser.Modules.CMS.Models;
using System.Web.Script.Serialization;
using Kaiser.Extensions;
using Kaiser.Modules.CMS.Services;
using Kaiser.Modules.CMS.Models;
namespace Kaiser.Modules.CMS.Controllers
{
    public class ContentController : Controller
    {
        private ICMSManageService _icmsManageService;
        public ContentController(ICMSManageService cmsManageService)
        {
            if (cmsManageService != null) _icmsManageService = cmsManageService;
        }


        public ActionResult ContentList()
        {
            return View();
        }


        /// <summary>
        /// 内容列表页面查询
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ContentListFunction(CategorySearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._icmsManageService.QueryContent(search, pageinfo);
            return Content(result.ToJsonString());
        }



[ValidateInput(false)]
        [HttpPost]
        public ActionResult AddContentFunction(ContentInput model, FormCollection form)
        {
       
            return EditContentFunction("", model, form);
        }

        public ActionResult AddContentFunction(string categoryCode)
        {
            var model = new ContentInput();
            model.ContentDetail = "";
            return View("EditContent", model);
        }

        public ActionResult EditContentFunction(string id, string categoryCode)
        {
            ContentInput model = _icmsManageService.GetContent(id);
            return View("EditContent", model);
        }

 [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditContentFunction(string id, ContentInput model, FormCollection form)
        {
            var result = new JsonReturnMessages();
            try
            {
                if (id.IsNullOrWhiteSpace())
                {
                    model.CreateTime = DateTime.Now;
                    model.IsRecommend = 0;
                    model.ClickCount = 0;
                    model.IsDelete = false;
                    model.UpdateTime = DateTime.Now;
                    model.CreateTime = DateTime.Now;

                    model.ContentDetail = Server.UrlDecode(model.ContentDetail);
                    model.ContentSummary = Server.UrlDecode(model.ContentSummary);
                    var list = new List<CategoryContentInput>();
                    foreach (string item in form["listCategoryContentInput"].Trim(',').Split(','))
                    {
                        list.Add(new CategoryContentInput { CategoryId = Convert.ToInt32(item) });
                    }
                    _icmsManageService.InsertContent(model, list);
                }
                else
                {
                  

                    model.UpdateTime = DateTime.Now;
                    List<CategoryContentInput> list = new List<CategoryContentInput>();
                    foreach (string item in form["listCategoryContentInput"].Trim(',').Split(','))
                    {
                        list.Add(new CategoryContentInput { CategoryId = Convert.ToInt32(item) });
                    }
                    model = _icmsManageService.GetContent(model.Id.ToString());
                    UpdateModel(model, form);
                    model.ContentDetail = Server.UrlDecode(model.ContentDetail);
                    model.ContentSummary = Server.UrlDecode(model.ContentSummary);

                    _icmsManageService.UpdateContent(model, list);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }



 
        public ActionResult DelContentFunction(string ids)
        {

            JsonReturnMessages result = new JsonReturnMessages();
            try
            {
                if (!ids.IsNullOrWhiteSpace())
                {
                    _icmsManageService.DeleteContentList(ids);
                    result.success = true;
                    result.msg = "删除成功";
                }
                else
                {
                    result.success = false;
                    result.msg = "传值失败，请告诉开发者解决";
                }
            }
            catch
            {
                result.msg = "传值失败，请告诉开发者解决";
                result.success = false;
            }

            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

    }
}
